package ysq.xyg.recovery.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.StringUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;
import ysq.xyg.base.model.dto.ReturnEntity;
import ysq.xyg.base.service.impl.BaseServiceImpl;
import ysq.xyg.base.util.BaseConstant;
import ysq.xyg.base.util.BaseUtil;
import ysq.xyg.recovery.dao.RecoveryRecordDao;
import ysq.xyg.recovery.model.po.RecoveryRecord;
import ysq.xyg.recovery.model.vo.RecoveryRecordVo;
import ysq.xyg.recovery.service.RecoveryRecordService;
import ysq.xyg.sys.model.po.SysUser;
import ysq.xyg.web.model.po.WebMember;
import ysq.xyg.web.service.WebMemberService;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * 回收金模块-回收记录表service实现
 *
 * @author congcong tao
 * @date 2019-5-8 15:12:49
 */
@Service("recoveryRecordService")
public class RecoveryRecordServiceImpl extends BaseServiceImpl<RecoveryRecord> implements RecoveryRecordService {

    @Autowired
    private WebMemberService webMemberService;
    @Resource
    private RecoveryRecordDao recoveryRecordDao;

    private RecoveryRecord recoveryRecord;

    @Autowired
    public void setBaseDao(RecoveryRecordDao recoveryRecordDao) {
        this.baseDao = recoveryRecordDao;
    }

    /**
     * 分页查询
     *
     * @param recoveryRecordVo 对象
     * @param pageSize 展示数量
     * @param pageNum 当前页面
     * @return ReturnEntity 对象
     * @author congcong tao
     * @date 2019-5-8 15:12:49
     */
    @Override
    public ReturnEntity page(RecoveryRecordVo recoveryRecordVo, int pageSize, int pageNum) {
        PageHelper.startPage(pageNum,pageSize);
        PageHelper.orderBy(" update_time desc");
        List<RecoveryRecord> list = acceptList(recoveryRecordVo);
        // 时间
        if (list != null && list.size() > 0) {
            for (RecoveryRecord recoveryRecord : list) {
                recoveryRecord.setCreateTimeStr(DateUtil.formatDateTime(recoveryRecord.getCreateTime()));
            }
        }
        PageInfo<RecoveryRecord> page = new PageInfo<RecoveryRecord>(list);
        return new ReturnEntity(page.getList(),page.getTotal(),page.isHasNextPage());
    }

    @Override
    public ReturnEntity pageLeft(RecoveryRecordVo recoveryRecordVo, int pageSize, int pageNum) {
        PageHelper.startPage(pageNum,pageSize);
        if (StringUtil.isNotEmpty(recoveryRecordVo.getMemberNickName())) {
            recoveryRecordVo.setMemberNickName(BaseUtil.encodeBase64String(recoveryRecordVo.getMemberNickName()));
        }
        if (StringUtil.isNotEmpty(recoveryRecordVo.getCmemberNickName())) {
            recoveryRecordVo.setCmemberNickName(BaseUtil.encodeBase64String(recoveryRecordVo.getCmemberNickName()));
        }
        List<RecoveryRecord> list = recoveryRecordDao.selectEntity(recoveryRecordVo);
        if (list != null && list.size() > 0) {
            for (RecoveryRecord recoveryRecord : list) {
                if (StringUtils.isNotEmpty(recoveryRecord.getMemberNickName())) {
                    recoveryRecord.setMemberNickName(BaseUtil.decodeBase64(recoveryRecord.getMemberNickName()));
                }
                if (StringUtils.isNotEmpty(recoveryRecord.getCmemberNickName())) {
                    recoveryRecord.setCmemberNickName(BaseUtil.decodeBase64(recoveryRecord.getCmemberNickName()));
                }
            }
        }
        PageInfo<RecoveryRecord> page = new PageInfo<RecoveryRecord>(list);
        return new ReturnEntity(page.getList(),page.getTotal(),page.isHasNextPage());
    }

    @Override
    public ReturnEntity pageByIntegral(RecoveryRecordVo recoveryRecordVo, int pageSize, int pageNum) {
        PageHelper.startPage(pageNum,pageSize);
        if (StringUtil.isNotEmpty(recoveryRecordVo.getMemberNickName())) {
            recoveryRecordVo.setMemberNickName(BaseUtil.encodeBase64String(recoveryRecordVo.getMemberNickName()));
        }
        List<RecoveryRecord> list = recoveryRecordDao.selectEntityByIntegral(recoveryRecordVo);
        if (list != null && list.size() > 0) {
            for (RecoveryRecord recoveryRecord : list) {
                recoveryRecord.setCreateTimeStr(DateUtil.formatDateTime(recoveryRecord.getCreateTime()));
                if (StringUtils.isNotEmpty(recoveryRecord.getMemberNickName())) {
                    recoveryRecord.setMemberNickName(BaseUtil.decodeBase64(recoveryRecord.getMemberNickName()));
                }
            }
        }
        PageInfo<RecoveryRecord> page = new PageInfo<RecoveryRecord>(list);
        return new ReturnEntity(page.getList(),page.getTotal(),page.isHasNextPage());
    }

    /**
     * 修改
     *
     * @param recoveryRecordVo 对象
     * @param currentUser 当前用户
     * @return ReturnEntity 对象
     * @author congcong tao
     * @date 2019-5-8 15:12:49
     */
    @Override
    public ReturnEntity addOrUpdate(RecoveryRecordVo recoveryRecordVo, SysUser currentUser) {
        boolean isUpate=true;
        if(StrUtil.isEmpty(recoveryRecordVo.getId())){
            recoveryRecord=new RecoveryRecord(currentUser);
            isUpate=false;
        }else{
            recoveryRecord=selectByPrimaryKey(recoveryRecordVo.getId());
            if(ObjectUtil.isNull(recoveryRecord)){
                return new ReturnEntity(BaseConstant.FAIL,BaseConstant.DO_FAIL);
            }
            recoveryRecord.setUpdateTime(new Date());
            recoveryRecord.setUpdateUser(currentUser.getId());
        }
        recoveryRecord.setName(recoveryRecordVo.getName());
        recoveryRecord.setType(recoveryRecordVo.getType());
        recoveryRecord.setMoney(recoveryRecordVo.getMoney());
        recoveryRecord.setDescription(recoveryRecordVo.getDescription());
        recoveryRecord.setStatus(recoveryRecordVo.getStatus());
        int num=0;
        if(isUpate){
            num=updateByPrimaryKey(recoveryRecord);
        }else{
            num=insert(recoveryRecord);
        }
        if(num<=0){
            return new ReturnEntity(BaseConstant.FAIL,BaseConstant.DO_FAIL);
        }
        return new ReturnEntity();
    }

    /**
     * 删除
     *
     * @param id 主键id
     * @param currentUser 当前用户
     * @return ReturnEntity 对象
     * @author congcong tao
     * @date 2019-5-8 15:12:49
     */
    @Override
    public ReturnEntity delete(String id, SysUser currentUser) {
        RecoveryRecord recoveryRecord=selectByPrimaryKey(id);
        if(recoveryRecord!=null){
            recoveryRecord.setRemoveTag(BaseConstant.BASE_REMOVE_TAG_DELETE);
            recoveryRecord.setUpdateTime(new Date());
            recoveryRecord.setUpdateUser(currentUser.getId());
            int num=updateByPrimaryKey(recoveryRecord);
            if(num>0){
                return new ReturnEntity();
            }
        }
        return new ReturnEntity(BaseConstant.FAIL,BaseConstant.DO_FAIL);
    }

    /**
     * 获取集合
     *
     * @param recoveryRecordVo 对象
     * @return List<BaseDict> BaseDict集合
     * @author congcong tao
     * @date 2019-5-8 15:12:49
     */
    @Override
    public List<RecoveryRecord> acceptList(RecoveryRecordVo recoveryRecordVo){
        Example example = new Example(RecoveryRecord.class);
        Example.Criteria criteria = example.createCriteria();
        if (StringUtil.isNotEmpty(recoveryRecordVo.getRemoveTag())) {
            criteria.andEqualTo("removeTag",recoveryRecordVo.getRemoveTag());
        }
        if (StringUtil.isNotEmpty(recoveryRecordVo.getType())) {
            criteria.andEqualTo("type",recoveryRecordVo.getType());
        }
        if (StringUtil.isNotEmpty(recoveryRecordVo.getMemberId())) {
            criteria.andEqualTo("memberId",recoveryRecordVo.getMemberId());
        }
        example.setOrderByClause(" update_time desc");
        return recoveryRecordDao.selectByExample(example);
    }

    @Override
    public ReturnEntity saveRecoveryRecord(RecoveryRecordVo recoveryRecordVo) {
        // 需要对webMember回收金控制
        String moneyStr = recoveryRecordVo.getMoney();
        if (StringUtils.isEmpty(moneyStr)) {
            return new ReturnEntity(BaseConstant.FAIL, "未传入用户");
        }
        Double money = Double.parseDouble(moneyStr);
        // 判断是否有足够金额消费
        WebMember webMember = webMemberService.selectByPrimaryKey(recoveryRecordVo.getMemberId());
        if (webMember == null) {
            return new ReturnEntity(BaseConstant.FAIL,"会员信息错误");
        }
        String type = recoveryRecordVo.getType();
        if (StringUtils.equals(type, BaseConstant.CONSUME_TYPE_PERMISSION)) {
            //消费
            if (money > webMember.getMoney()) {
                return new ReturnEntity(BaseConstant.FAIL, "您拥有的金额不够");
            }
            webMember.setMoney(webMember.getMoney() - money);
        } else {
            //回收
            webMember.setMoney(webMember.getMoney() + money);
        }
        int num = webMemberService.updateByPrimaryKey(webMember);
        if (num > 0) {
            /*RecoveryRecord recoveryRecord = new RecoveryRecord();
            BeanUtil.copyProperties(recoveryRecordVo, recoveryRecord);
            recoveryRecord.setId(BaseUtil.uId());
            recoveryRecord.setCreateTime(DateUtil.date());
            recoveryRecord.setRemoveTag(BaseConstant.BASE_REMOVE_TAG_NORMAL);
            recoveryRecord.setUpdateTime(recoveryRecord.getCreateTime());
            int info = this.insert(recoveryRecord);
            if (info <= 0) {
                return new ReturnEntity(BaseConstant.FAIL,BaseConstant.DO_FAIL);
            }*/
        } else {
            return new ReturnEntity(BaseConstant.FAIL,BaseConstant.DO_FAIL);
        }
        return new ReturnEntity();
    }

    @Override
    public Double selectAllCount(RecoveryRecord recoveryRecord) {
        return recoveryRecordDao.selectAllCount(recoveryRecord);
    }

    @Override
    public Double selectWeightCount(RecoveryRecord recoveryRecord) {
        return recoveryRecordDao.selectWeightCount(recoveryRecord);
    }
}
